package com.accloud.cloudservice;

import com.accloud.common.ACConfiguration;
import com.accloud.common.ACConstant;
import com.accloud.service.ACBindMgr;
import com.accloud.service.ACDeviceFind;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACDeviceSecurityMode;
import com.accloud.service.ACDeviceUser;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.ACObject;
import com.accloud.service.ACUserDevice;
import com.accloud.utils.LogUtil;
import com.accloud.utils.PreferencesUtils;
import com.loopj.android.http.AsyncHttpClient;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;

public class ACBindManager extends ACBaseManager implements ACBindMgr {
    private static final String KEY_DEVICE_NUM = "ablecloud_privatedevices_num";

    public ACBindManager() {
        super(ACConfiguration.BIND_SERVICE_NAME, 1, false);
    }

    @Override // com.accloud.service.ACBindMgr
    public void listDevices(PayloadCallback<List<ACUserDevice>> callback) {
        listDevices(callback, false);
    }

    @Override // com.accloud.service.ACBindMgr
    public void listDevicesWithStatus(PayloadCallback<List<ACUserDevice>> callback) {
        listDevices(callback, true);
    }

    @Override // com.accloud.service.ACBindMgr
    public void getDeviceCount(int subDomainId, final PayloadCallback<Integer> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg(null);
        req.setName("getDeviceCountOfUser");
        if (subDomainId > 0) {
            req.put("subDomainId", String.valueOf(subDomainId));
        }
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACBindManager.AnonymousClass1 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                callback.success(Integer.valueOf(resp.getInt("count")));
            }
        });
    }

    @Override // com.accloud.service.ACBindMgr
    public void getDevices(int subDomainId, int offset, int limit, final PayloadCallback<List<ACUserDevice>> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("getDevicesOfUserExt");
        if (subDomainId > 0) {
            req.put("subDomainId", String.valueOf(subDomainId));
        }
        req.put("offset", Integer.valueOf(offset));
        req.put("limit", Integer.valueOf(limit));
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACBindManager.AnonymousClass2 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                List<ACObject> objects = resp.getList("devices");
                ArrayList arrayList = new ArrayList();
                for (ACObject object : objects) {
                    ACUserDevice userDevice = new ACUserDevice(object.getLong("deviceId"), object.getLong("owner"), object.getString("name"), object.getLong("subDomainId"), object.getString("subDomainName"), object.getString("aesKey"), object.getString("physicalDeviceId"));
                    userDevice.setAttributes(object.getACObject("attributes"));
                    userDevice.setProfiles(object.getACObject("profiles"));
                    userDevice.setStatus((int) object.getLong("status"));
                    arrayList.add(userDevice);
                }
                callback.success(arrayList);
            }
        });
    }

    private void listDevices(final PayloadCallback<List<ACUserDevice>> callback, final boolean withStatus) {
        BaseCallback baseCallback;
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("listDevicesExt");
        req.put("status", Boolean.valueOf(withStatus));
        if (!withStatus) {
            baseCallback = callback;
        } else {
            baseCallback = new BaseCallback() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass3 */

                @Override // com.accloud.cloudservice.BaseCallback
                public void error(ACException e) {
                    if (e.getErrorCode() < 3000 || e.getErrorCode() > 10000) {
                        ACBindManager.this.listDevicesFromCacheWithStatus(callback);
                    } else {
                        callback.error(e);
                    }
                }

                @Override // com.accloud.cloudservice.BaseCallback
                public void onFinish() {
                    callback.onFinish();
                }
            };
        }
        sendReq(req, baseCallback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACBindManager.AnonymousClass4 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                List<ACObject> userDevices = resp.getList("devices");
                ArrayList arrayList = new ArrayList();
                int deviceNum = userDevices.size();
                PreferencesUtils.putInt(AC.context, ACBindManager.KEY_DEVICE_NUM, deviceNum);
                for (int i = 0; i < deviceNum; i++) {
                    ACObject device = userDevices.get(i);
                    ACUserDevice userDevice = new ACUserDevice(device.getLong("deviceId"), device.getLong("owner"), device.getString("name"), device.getLong("subDomainId"), device.getString("subDomainName"), device.getString("aesKey"), device.getString("physicalDeviceId"));
                    userDevice.setAttributes(device.getACObject("attributes"));
                    userDevice.setProfiles(device.getACObject("profiles"));
                    if (withStatus) {
                        long status = device.getLong("status");
                        if (ACConstant.deviceFinds != null && ACConstant.deviceFinds.size() > 0) {
                            for (ACDeviceFind deviceFind : ACConstant.deviceFinds) {
                                if (deviceFind.getPhysicalDeviceId().equals(userDevice.getPhysicalDeviceId())) {
                                    status = status == 0 ? 2 : 3;
                                }
                            }
                        }
                        userDevice.setStatus((int) status);
                    }
                    arrayList.add(userDevice);
                    ACBindManager.this.saveUserDevice(userDevice, i);
                }
                callback.success(arrayList);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void saveUserDevice(ACUserDevice acUserDevice, int i) {
        PreferencesUtils.putString(AC.context, "ablecloud_privatedevice" + i + "_aesKey", acUserDevice.getAesKey());
        PreferencesUtils.putLong(AC.context, "ablecloud_privatedevice" + i + "_deviceId", acUserDevice.getDeviceId());
        PreferencesUtils.putLong(AC.context, "ablecloud_privatedevice" + i + "_owner", acUserDevice.getOwner());
        PreferencesUtils.putString(AC.context, "ablecloud_privatedevice" + i + "_physicalDeviceId", acUserDevice.getPhysicalDeviceId());
        PreferencesUtils.putLong(AC.context, "ablecloud_privatedevice" + i + "_subDomainId", acUserDevice.getSubDomainId());
        PreferencesUtils.putString(AC.context, "ablecloud_privatedevice" + i + "_subDomain", acUserDevice.getSubDomain());
        PreferencesUtils.putString(AC.context, "ablecloud_privatedevice" + i + "_deviceName", acUserDevice.getName());
    }

    private ACUserDevice restoreUserDevice(int i) {
        return new ACUserDevice(PreferencesUtils.getLong(AC.context, "ablecloud_privatedevice" + i + "_deviceId", 0), PreferencesUtils.getLong(AC.context, "ablecloud_privatedevice" + i + "_owner", 0), PreferencesUtils.getString(AC.context, "ablecloud_privatedevice" + i + "_deviceName"), PreferencesUtils.getLong(AC.context, "ablecloud_privatedevice" + i + "_subDomainId", 0), PreferencesUtils.getString(AC.context, "ablecloud_privatedevice" + i + "_subDomain"), PreferencesUtils.getString(AC.context, "ablecloud_privatedevice" + i + "_aesKey"), PreferencesUtils.getString(AC.context, "ablecloud_privatedevice" + i + "_physicalDeviceId"));
    }

    public void listDevicesFromCacheWithStatus(PayloadCallback<List<ACUserDevice>> callback) {
        List<ACUserDevice> devices = new ArrayList<>();
        int number = PreferencesUtils.getInt(AC.context, KEY_DEVICE_NUM, 0);
        for (int i = 0; i < number; i++) {
            ACUserDevice userDevice = restoreUserDevice(i);
            if (ACConstant.deviceFinds != null && ACConstant.deviceFinds.size() > 0) {
                for (ACDeviceFind deviceFind : ACConstant.deviceFinds) {
                    if (userDevice.getPhysicalDeviceId().equals(deviceFind.getPhysicalDeviceId())) {
                        userDevice.setStatus(2);
                    }
                }
            }
            devices.add(userDevice);
        }
        callback.success(devices);
    }

    @Override // com.accloud.service.ACBindMgr
    public boolean isDeviceLocalOnline(String physicalDeviceId) {
        if (physicalDeviceId.length() <= 0 || ACConstant.deviceFinds == null || ACConstant.deviceFinds.size() <= 0) {
            return false;
        }
        for (ACDeviceFind deviceFind : ACConstant.deviceFinds) {
            if (deviceFind.getPhysicalDeviceId().equals(physicalDeviceId)) {
                return true;
            }
        }
        return false;
    }

    @Override // com.accloud.service.ACBindMgr
    public void listUsers(String subDomain, long deviceId, final PayloadCallback<List<ACDeviceUser>> callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("listUsers");
            req.put("deviceId", Long.valueOf(deviceId));
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass5 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    List<ACObject> acObjects = resp.getList("users");
                    ArrayList arrayList = new ArrayList();
                    for (ACObject binding : acObjects) {
                        arrayList.add(new ACDeviceUser(binding.getLong("userId"), binding.getLong("deviceId"), binding.getLong("userType"), binding.getString("phone"), binding.getString("email"), binding.getString("nickName"), binding.getACObject("profile")));
                    }
                    callback.success(arrayList);
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void bindDevice(String subDomain, String physicalDeviceId, String name, PayloadCallback<ACUserDevice> callback) {
        final PayloadEventCallback<ACUserDevice> callbackWrapper = new PayloadEventCallback<>(new BindEvent(subDomain, physicalDeviceId, null), callback);
        if (physicalDeviceId.length() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("bindDevice");
            req.put("physicalDeviceId", physicalDeviceId);
            if (name != null) {
                req.put("name", name);
            }
            sendReq(req, callbackWrapper, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass6 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    ACObject device = (ACObject) resp.get("device");
                    if (device == null) {
                        callbackWrapper.error(new ACException(ACException.INTERNAL_ERROR));
                        return;
                    }
                    callbackWrapper.success(new ACUserDevice(device.getLong("deviceId"), device.getLong("owner"), device.getString("name"), device.getLong("subDomainId"), device.getString("subDomainName"), device.getString("aesKey"), device.getString("physicalDeviceId")));
                    AC.findLocalDevice(AsyncHttpClient.DEFAULT_RETRY_SLEEP_TIME_MILLIS, new PayloadCallback<List<ACDeviceFind>>() {
                        /* class com.accloud.cloudservice.ACBindManager.AnonymousClass6.AnonymousClass1 */

                        public void success(List<ACDeviceFind> list) {
                        }

                        @Override // com.accloud.cloudservice.BaseCallback
                        public void error(ACException e) {
                        }
                    });
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void bindDeviceWithShareCode(String shareCode, PayloadCallback<ACUserDevice> callback) {
        final PayloadEventCallback<ACUserDevice> callbackWrapper = new PayloadEventCallback<>(new BindEvent(shareCode), callback);
        if (shareCode.length() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("bindDeviceWithShareCode");
            req.put("shareCode", shareCode);
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass7 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    ACObject device = (ACObject) resp.get("device");
                    if (device == null) {
                        callbackWrapper.error(new ACException(ACException.INTERNAL_ERROR));
                        return;
                    }
                    callbackWrapper.success(new ACUserDevice(device.getLong("deviceId"), device.getLong("owner"), device.getString("name"), device.getLong("subDomainId"), device.getString("subDomainName"), device.getString("aesKey"), device.getString("physicalDeviceId")));
                    AC.findLocalDevice(AsyncHttpClient.DEFAULT_RETRY_SLEEP_TIME_MILLIS, new PayloadCallback<List<ACDeviceFind>>() {
                        /* class com.accloud.cloudservice.ACBindManager.AnonymousClass7.AnonymousClass1 */

                        public void success(List<ACDeviceFind> list) {
                        }

                        @Override // com.accloud.cloudservice.BaseCallback
                        public void error(ACException e) {
                        }
                    });
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void bindDeviceWithUser(String subDomain, long deviceId, String account, VoidCallback callback) {
        VoidEventCallback callbackWrapper = new VoidEventCallback(new ShareEvent(subDomain, Long.valueOf(deviceId), account), callback);
        if (deviceId <= 0 || account.length() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("bindDeviceWithUser");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("account", account);
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void unbindDevice(String subDomain, long deviceId, VoidCallback callback) {
        VoidEventCallback callbackWrapper = new VoidEventCallback(new UnbindEvent(subDomain, Long.valueOf(deviceId), "normal"), callback);
        if (deviceId <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("unbindDevice");
            req.put("deviceId", Long.valueOf(deviceId));
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void unbindDeviceWithUser(String subDomain, long userId, long deviceId, VoidCallback callback) {
        VoidEventCallback callbackWrapper = new VoidEventCallback(new UnshareEvent(subDomain, deviceId, Long.valueOf(userId)), callback);
        if (deviceId <= 0 || userId <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("unbindDeviceWithUser");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("userId", Long.valueOf(userId));
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void getShareCode(String subDomain, long deviceId, PayloadCallback<String> callback) {
        getShareCode(subDomain, deviceId, 0, callback);
    }

    @Override // com.accloud.service.ACBindMgr
    public void getShareCode(String subDomain, long deviceId, int timeout, final PayloadCallback<String> callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("getShareCode");
            req.put("deviceId", Long.valueOf(deviceId));
            if (timeout > 0) {
                req.put("timeout", Integer.valueOf(timeout));
            }
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass8 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callback.success(resp.getString("shareCode"));
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void fetchShareCode(long deviceId, int timeout, final PayloadCallback<String> callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(null);
            req.setName("fetchDeviceShareCode");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("timeout", Integer.valueOf(timeout));
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass9 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callback.success(resp.getString("shareCode"));
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void refreshShareCode(long deviceId, int timeout, final PayloadCallback<String> callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(null);
            req.setName("refreshDeviceShareCode");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("timeout", Integer.valueOf(timeout));
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass10 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callback.success(resp.getString("shareCode"));
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void changeOwner(String subDomain, long deviceId, long userId, VoidCallback callback) {
        VoidEventCallback callbackWrapper = new VoidEventCallback(new ChangeEvent(subDomain, deviceId, userId), callback);
        if (deviceId <= 0 || userId <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("changeOwner");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("userId", Long.valueOf(userId));
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void changeDevice(String subDomain, String physicalDeviceId, long deviceId, VoidCallback callback) {
        if (physicalDeviceId.length() <= 0 || deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("changeDevice");
            req.put("physicalDeviceId", physicalDeviceId);
            req.put("deviceId", Long.valueOf(deviceId));
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void changeName(String subDomain, long deviceId, String name, VoidCallback callback) {
        if (deviceId <= 0 || name.length() <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("modifyDevice");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("name", name);
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void isDeviceOnline(String subDomain, long deviceId, String physicalDeviceId, final PayloadCallback<Boolean> callback) {
        if (deviceId <= 0 && physicalDeviceId.length() <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("isDeviceOnline");
            if (deviceId > 0) {
                req.put("deviceId", Long.valueOf(deviceId));
            }
            if (physicalDeviceId.length() > 0) {
                req.put("physicalDeviceId", physicalDeviceId);
            }
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass11 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callback.success(Boolean.valueOf(resp.getBoolean("online")));
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void isDeviceBound(String physicalDeviceId, PayloadCallback<Boolean> callback) {
        isDeviceBound(null, physicalDeviceId, callback);
    }

    @Override // com.accloud.service.ACBindMgr
    @Deprecated
    public void isDeviceBound(String subDomain, String physicalDeviceId, final PayloadCallback<Boolean> callback) {
        if (physicalDeviceId.length() <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("isDeviceBound");
            req.put("physicalDeviceId", physicalDeviceId);
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass12 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callback.success(Boolean.valueOf(resp.getBoolean("bound")));
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void resetDeviceToken(String subDomain, long deviceId, final VoidCallback callback) {
        if (subDomain.length() <= 0 && deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("updateAccessKey");
            req.put("deviceId", Long.valueOf(deviceId));
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass13 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    long deviceId = resp.getLong("deviceId");
                    String aesKey = resp.getString("aesKey");
                    for (int i = 0; i < PreferencesUtils.getInt(AC.context, ACBindManager.KEY_DEVICE_NUM, 0); i++) {
                        if (deviceId == PreferencesUtils.getLong(AC.context, "ablecloud_privatedevice" + i + "_deviceId", 0)) {
                            PreferencesUtils.putString(AC.context, "ablecloud_privatedevice" + i + "_aesKey", aesKey);
                        }
                    }
                    callback.success();
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void setDeviceProfile(String subDomain, long deviceId, ACObject deviceProfile, VoidCallback callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("setDeviceProfile");
            req.put("deviceId", Long.valueOf(deviceId));
            req.put("deviceProfile", deviceProfile);
            sendReq(req, callback);
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void getDeviceProfile(String subDomain, long deviceId, final PayloadCallback<ACObject> callback) {
        if (deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("getDeviceProfile");
            req.put("deviceId", Long.valueOf(deviceId));
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass14 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callback.success(resp.getACObject("deviceProfile"));
                }
            });
        }
    }

    @Override // com.accloud.service.ACBindMgr
    public void sendToDeviceWithOption(final String subDomain, final String physicalDeviceId, final ACDeviceMsg msg, int option, final PayloadCallback<ACDeviceMsg> callback) {
        if (physicalDeviceId.length() <= 0 || msg == null) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        }
        if (option == 1) {
            AC.deviceLocalManager.sendToDevice(physicalDeviceId, msg, AC.SEND_TO_LOCAL_DEVICE_DEFAULT_TIMEOUT, callback);
        } else if (option == 2) {
            sendToDevice(subDomain, physicalDeviceId, msg, callback);
        } else if (option == 4) {
            sendToDevice(subDomain, physicalDeviceId, msg, new PayloadCallback<ACDeviceMsg>() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass15 */

                public void success(ACDeviceMsg deviceMsg) {
                    callback.success(deviceMsg);
                }

                @Override // com.accloud.cloudservice.BaseCallback
                public void error(ACException ex) {
                    new Thread() {
                        /* class com.accloud.cloudservice.ACBindManager.AnonymousClass15.AnonymousClass1 */

                        @Override // java.lang.Thread, java.lang.Runnable
                        public void run() {
                            AC.deviceLocalManager.sendToDevice(physicalDeviceId, msg, AC.SEND_TO_LOCAL_DEVICE_DEFAULT_TIMEOUT, callback);
                        }
                    }.start();
                }
            });
        } else {
            AC.deviceLocalManager.sendToDevice(physicalDeviceId, msg, AC.SEND_TO_LOCAL_DEVICE_DEFAULT_TIMEOUT, new PayloadCallback<ACDeviceMsg>() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass16 */

                public void success(ACDeviceMsg deviceMsg) {
                    callback.success(deviceMsg);
                }

                @Override // com.accloud.cloudservice.BaseCallback
                public void error(ACException e) {
                    ACBindManager.this.sendToDevice(subDomain, physicalDeviceId, msg, callback);
                }
            });
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void sendToDevice(String subDomain, String physicalDeviceId, ACDeviceMsg msg, PayloadCallback<ACDeviceMsg> callback) {
        final PayloadEventCallback<ACDeviceMsg> callbackWrapper = new PayloadEventCallback<>(new ControlEvent(ControlEvent.CLOUD, subDomain, physicalDeviceId, msg), callback);
        if (msg.getCode() < 64 || msg.getCode() >= 200) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid code, range should be in [64,200)"));
        } else if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg(subDomain);
            req.setName("sendToDevice");
            req.put("subDomain", subDomain);
            req.put("physicalDeviceId", physicalDeviceId);
            req.put("messageCode", Integer.valueOf(msg.getCode()));
            LogUtil.d("sendToDevice msg-->"+msg.toString());
            if (msg.getDescription() != null) {
                req.put("description", msg.getDescription());
            }
            byte[] payload = msg.getContent();
            if (payload != null) {
                req.setStreamPayload(new ByteArrayInputStream(payload), payload.length);
            } else {
                req.setStreamPayload(null, 0);
            }
            sendReq(req, callbackWrapper, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACBindManager.AnonymousClass17 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    try {
                        callbackWrapper.success(new ACDeviceMsg(Integer.parseInt(resp.getString("code")), resp.getPayload()));
                    } catch (NumberFormatException e) {
                        callbackWrapper.error(new ACException(ACException.INTERNAL_ERROR, e.toString()));
                    }
                }
            });
        }
    }

    /* access modifiers changed from: package-private */
    public String getAESKey(String physicalDeviceId, ACDeviceSecurityMode mode) throws ACException {
        switch (mode) {
            case DYNAMIC_ENCRYPTED:
                int num = PreferencesUtils.getInt(AC.context, KEY_DEVICE_NUM, -1);
                for (int i = 0; i < num; i++) {
                    ACUserDevice device = restoreUserDevice(i);
                    if (device.physicalDeviceId.equals(physicalDeviceId)) {
                        return device.getAesKey();
                    }
                }
                throw new ACException(ACException.ENTRY_EMPTY, "please make sure you have bound this device first");
            case STATIC_ENCRYPTED:
                if (physicalDeviceId.length() > 16) {
                    return physicalDeviceId.substring(0, 16);
                }
                return "0000000000000000".substring(0, 16 - physicalDeviceId.length()) + physicalDeviceId;
            default:
                return null;
        }
    }

    private static class BindEvent extends ACEvent {
        private BindEvent(String subDomain, String physicalDeviceId, Long deviceId) {
            super("apm_app_device_manage", "bind", null);
            try {
                this.subDomain = subDomain;
                this.physicalDeviceId = physicalDeviceId;
                this.deviceId = deviceId;
                this.data.put("user_type", "master");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        private BindEvent(String shareCode) {
            super("apm_app_device_manage", "bind", null);
            try {
                this.data.put("user_type", "normal");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private static class UnbindEvent extends ACEvent {
        private UnbindEvent(String subDomain, Long deviceId, String userType) {
            super("apm_app_device_manage", "unbind", null);
            try {
                this.subDomain = subDomain;
                this.deviceId = deviceId;
                this.data.put("user_type", userType);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private static class ShareEvent extends ACEvent {
        private ShareEvent(String subDomain, Long deviceId, String account) {
            super("apm_app_device_manage", "share", null);
            try {
                this.subDomain = subDomain;
                this.deviceId = deviceId;
                this.data.put("user_type", "master");
                this.data.put("share_to", account);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private static class UnshareEvent extends ACEvent {
        private UnshareEvent(String subDomain, long deviceId, Long userId) {
            super("apm_app_device_manage", "unbind", null);
            try {
                this.subDomain = subDomain;
                this.deviceId = Long.valueOf(deviceId);
                this.data.put("user_type", "master");
                this.data.put("unbind_uid", userId);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private static class ChangeEvent extends ACEvent {
        private ChangeEvent(String subDomain, long deviceId, long userId) {
            super("apm_app_device_manage", "change_owner", null);
            try {
                this.subDomain = subDomain;
                this.deviceId = Long.valueOf(deviceId);
                this.data.put("user_type", "master");
                this.data.put("change_to", userId);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
